<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/16
 * Time: 上午11:43
 */
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller
{

    public function index()
    {
        if(session(C('ADMIN_SESSION'))){
            $this->redirect('Index/index');
        }
        if(IS_AJAX){
            $username = I('post.username');
            if(empty($username)){
                $this->ajaxReturn(['status'=> 1,'msg'=>'用户名不能为空']);
            }
            $password = I('post.pswd');
            if(empty($password)){
                $this->ajaxReturn(['status'=> 1,'msg'=>'密码不能为空']);
            }
            $info = M('Admin')->where(['account'=>$username])->find();
            if(empty($info)){
                $this->ajaxReturn(['status'=> 1,'msg'=>'该用户不存在']);
            }
            if(md5($password.$info['salt']) != $info['password']){
                $this->ajaxReturn(['status'=> 1,'msg'=>'输入的账号或密码不正确']);
            }
            session(C('ADMIN_SESSION'),$info);
            $this->ajaxReturn(['status'=> 0,'msg'=>'登录成功']);
        }else{
            $this->display();
        }
    }

    public function login_out()
    {
        session(C('ADMIN_SESSION'),null);
        $this->redirect('Login/index');

    }
}